package managepictures;
import javax.imageio.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2016/10/18.
 */
public class Manage  {

    public void cut(BufferedImage image) {                                            //裁剪图片
        try{
            BufferedImage picture =image;                                            //读取文件
            int width=picture.getWidth();
            int height=picture.getHeight();                                         //获取图片的宽度和高度
            int[] imageArray=new int[width*height];                                 //创建数组用于存储像素

            picture.getRGB(0,0,width,height,imageArray,0,width);
            BufferedImage newPicture=new BufferedImage(width/3*2,height/3*2,BufferedImage.TYPE_INT_RGB);//创建新的对象，确定宽度和高度
            newPicture.setRGB(0,0,width/3*2,height/3*2,imageArray,0,width);          //将原图片的对应位置的像素点复制到新图片中

            File outFile=new File("src/picture1.jpg");
            ImageIO.write(newPicture,"jpg",outFile);                            //输出图片

            System.out.println("遍历像素：");
            for(int i=0;i<width/3*2;i++)
                for(int j=0;j<height/3*2;j++)
                    getARGB(newPicture.getRGB(i,j));        //遍历像素

           }catch(IOException e){
                     e.printStackTrace();
              }
    }//end cut

        public static int interpolation(int[] imageData, int width, int height,
                                        float x, float y) {                                //双线性插值算法计算像素点的位置
            // 四个最临近象素的坐标(i1, j1), (i2, j1), (i1, j2), (i2, j2)
            int i1, i2;
            int j1, j2;

            int f1, f2, f3, f4; // 四个最临近像素值
            int f12, f34; // 二个插值中间值

            f1 = f2 = f3 = f4 = 0;

            // 定义一个值，当像素坐标相差小于改值时认为坐标相同
            int EXP = 0;

            // 计算四个最临近象素的坐标
            i1 = (int) x;
            i2 = i1 + 1;
            j1 = (int) y;
            j2 = j1 + 1;

            // 根据不同情况分别处理
            if ((x < 0) || (x > width - 1) || (y < 0) || (y > height - 1)) {
                return 0x00ffffff; // 要计算的点不在源图范围内，直接返回255。
            } else {
                if (Math.abs(x - width + 1) <= EXP) {
                    // 要计算的点在图像右边缘上
                    if (Math.abs(y - height + 1) <= EXP) {
                        // 要计算的点正好是图像最右下角那一个象素，直接返回该点象素值
                        f1 = (int)imageData[width * j1 + i1];
                        return (int)f1;
                    } else {
                        // 在图像右边缘上且不是最后一点，直接一次插值即可
                        f1 = (int)imageData[width * j1 + i1];
                        f3 = (int)imageData[width * j1 + i2];

                        // 返回插值结果
                        return (int) (f1 + (y - j1) * (f3 - f1));
                    }
                } else if (Math.abs(y - height + 1) <= EXP) {
                    // 要计算的点在图像下边缘上且不是最后一点，直接一次插值即可
                    f1 = (int)imageData[width * j1 + i1];
                    f2 = (int)imageData[width * j2 + i1];

                    // 返回插值结果
                    return (int) (f1 + (x - i1) * (f2 - f1));
                } else {
                    // 计算四个最临近象素值
                    f1 = imageData[width * j1 + i1];
                    f2 = imageData[width * j1 + i2];
                    f3 = imageData[width * j2 + i1];
                    f4 = imageData[width * j2 + i2];

                     //插值1
                    f12 = (int)(f1 + (x - i1) * (f2 - f1));
                    // 插值2
                    f34 = (int)((f3 + (x - i1) * (f4 - f3)));
                    // 插值3
                    return  (int)(f12 + (y - j1) * (f34 - f12));
                }
            }

        }//end interpolation

    public void change(BufferedImage file,int DestWidth,int DestHeight)      //改变图片大小
    {
        try {
            BufferedImage src=file;
            int destW = DestWidth; //目标图片的宽度
            int destH = DestHeight; //目标图片的高度

            int srcW = src.getWidth();
            int srcH = src.getHeight();

            // 创建目标图片的像素数组
            int[] destPixels = new int[destW * destH];
            int[] destRed = new int[destW * destH];
            int[] destGreen = new int[destW * destH];
            int[] destBlue = new int[destW * destH];

            // 创建原图片的像素数组
            int[] srcPixels = new int[srcW * srcH];
            int[] srcRed = new int[srcW * srcH];
            int[] srcGreen = new int[srcW * srcH];
            int[] srcBlue = new int[srcW * srcH];
            src.getRGB( 0, 0, srcW, srcH,srcPixels, 0, srcW);

            for (int i = 0; i < srcPixels.length; i++) {
                srcBlue[i] = srcPixels[i]&0x000000ff;
                srcGreen[i] = (srcPixels[i]>>8)&0x000000ff;
                srcRed[i] = (srcPixels[i]>>16)&0x000000ff;
            }//end for

            // 利用算法计算目标图片中的像素点对应原图片的像素点的位置
            for (int destY = 0; destY < destH; ++destY) {
                for (int destX = 0; destX < destW; ++destX) {
                    float srcX = ((float)destX * (float)srcW) / (float)destW;
                    float srcY = ((float)destY * (float)srcH) / (float)destH;
                    try {
                        destRed[destX + destY * destW] = interpolation(srcRed, srcW, srcH, srcX, srcY);
                        destGreen[destX + destY * destW] = interpolation(srcGreen, srcW, srcH, srcX, srcY);
                        destBlue[destX + destY * destW] = interpolation(srcBlue, srcW, srcH, srcX, srcY);

                        int value = 0xff000000;
                        value|=destBlue[destX + destY * destW];
                        value|=(destGreen[destX + destY * destW]<<8);
                        value|=(destRed[destX + destY * destW]<<16);

                        destPixels[destX + destY * destW] = value;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }//end catch
                }//end for
            }//end for

            BufferedImage picture2=new BufferedImage(destW,destH,BufferedImage.TYPE_INT_RGB);
            picture2.setRGB(0,0,destW,destH,destPixels,0,destW);
            File outImage=new File("src/picture2.jpg");
            ImageIO.write(picture2,"jpg",outImage);       //输出图片

            System.out.println("遍历像素：");
            for(int i=0;i<destW;i++)
                for(int j=0;j<destH;j++)
                    getARGB(picture2.getRGB(i,j));        //遍历像素

        } catch (Exception e) {
            e.printStackTrace();
        }//end catch
    }//end change

    public static void getARGB(int pixel) {
        int alpha = (pixel >> 24) & 0xff;
        int red=(pixel >>16)&0xff;
        int green=(pixel >>16)&0xff;
        int blue =(pixel)&0xff;
        System.out.println("argb:"+alpha+","+red+","+green+","+blue+".");
    }//end getARGB
}//end Manage
